///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_APP_CREGISTRYFOLDER_H_
#define _BUOLA_APP_CREGISTRYFOLDER_H_

#include <buola/dbus/cproxy.h>
#include <buola/bbox/proxies.h>

namespace buola {

///\addtogroup registry
///@{

typedef std::map<std::string,UValue> CRegistryMap;
    
////////////////////////////////////////////////////////////////////////////

/// registry folder

/// Represents a folder in the configuration registry. It contains the
/// interface to access the values in this folder
///
////////////////////////////////////////////////////////////////////////////

class CRegistryFolder
{
///\name constructor and destructor
//@{
private:
    ///from a registry and a folder name (for internal use)
    CRegistryFolder(CRegistry*,const std::string&);
public:
    ///from the folder name
    explicit CRegistryFolder(const std::string &pFolder);

    ///copy constructor
    CRegistryFolder(const CRegistryFolder &pO)=default;
    CRegistryFolder &operator=(const CRegistryFolder &pRH)=default;
//@}

public:
//! \name accessing values
//@{
    ///returns the value corresponding to key \p pKey
    bool GetValue(const std::string &pKey,UValue &pValue);
    ///sets the value corresponding to key \p pKey
    void SetValue(const std::string &pKey,const UValue &pValue);

    //! gets the value of key \p pKey as type \p tType
    template<typename tType>
    tType Get(const std::string &pKey)
    {
        UValue lValue;
        if(!GetValue(pKey,lValue)) throw XNotFound("Not found registry key");
        return lValue.Get<tType>();
    }

    //! sets the value of key \p pKey to \p pValue
    template<typename tType>
    void Set(const std::string &pKey,const tType &pValue)
    {
        SetValue(pKey,UValue(pValue));
    }

    //! \brief gets the value of key \p pKey as type \p tType
    //!
    //! If the key doesn't exist, it returns pDefault, and optionally
    //! sets the key to this value.
    template<typename tType>
    tType Get(const std::string &pKey,const tType &pDefault,bool pSet)
    {
        UValue lValue;
        if(!GetValue(pKey,lValue))
        {
            if(pSet)
            {
                Set(pKey,UValue(pDefault));
            }
            return pDefault;
        }
        return lValue.Get<tType>();
    }

    //! gets a list of all keys in the folder
    std::vector<std::string> GetKeyList();
    //! gets a list of all key-value pairs in the folder
    CRegistryMap GetKeyValueMap();
//@}

protected:
    const bbox::BServiceProxy *mProxy;
    std::string mPath;

    friend class CRegistry;
};

///@}

/*namespace buola*/ }

#endif
